<?php
namespace Admin\Controller;
use Common\Controller\AuthController;
class TremController extends AuthController {

/**
 * 显示权限管理页面fcjList
 */
    public function index(){
        $this->display();
    }



    public function cjList(){
        $pid=$_GET['pid'];
        if($pid){
            $map['pid']=$pid;
        }else{
            $map['pid']=0;
        }
        $trem=M('trem');
        $tremlists=$trem->where($map)->order('id asc')->select();
        echo json_encode($tremlists);
    }

    public function cjListTree(){
        $trem=M('trem');
        $degx=M('degx');
        $tremlists=$trem->order('id asc')->select();
        $data=array();
        foreach($tremlists as $k=>$v){
            if($v['pid']==0){
                foreach($tremlists as $k1=>$v1){
                    if($v1['pid']==$v['id']){

                        $map['cj']=$v['id'];
                        $map['gw']=$v1['title'];
                        $num=$degx->where($map)->count();
                        if($num>0){
                            $v1['text']=$v1['title']."(".$num.")";
                        }else{
                            $v1['text']=$v1['title'];
                        }


                        $v['children'][]=$v1;


                    }
                }

                $v['text']=$v['title'];
                if($v['id']!=1){
                    $v['state']="closed";
                }

                $data[]=$v;
            }


        }
        echo json_encode($data);
    }


      public function fzhList(){
      
        $q = isset($_POST['q']) ? $_POST['q'] : '';

        $map['dc']=array('LIKE',array("%".$q."%"));
        $fzh=M('fzh');    
        $fzhlists=$fzh->where($map)->order('id asc')->select();
        echo json_encode($fzhlists);
    }
   
    /**
 * [权限权限数据]
 * @return [text] [description]
 */
    public function tremTree(){
        $trem=M('trem');    
        $tremlists=$trem->order('id asc')->select();
        echo json_encode(tree($tremlists));
    }
/**
 * [添加权限]
 * @return [text] [description]
 */
    public function addTrem(){
        $trem=M('trem');
        $trem -> create(); //收集post表单数据
        $result = $trem -> add();
        if($result){            
             echo '新建工位成功！';
        }
    }
/**
 * [删除权限]
 * @return [json] [description]
 */
public function del(){
    $trem=M('trem');
    $map['id']= $_POST['id'];  
    $data=$trem->where($map)->find();

    $mapa['pid']=$_POST['id'];
    $childnum=$trem->where($mapa)->count();

    if($childnum>0){
        $data['status']=0;
        $data['data']="不能删除包含子工位的工位！";
        echo json_encode($data);
    }else{
       $r=$trem->where($map)->delete(); 

        if($r){
            $data['status']=1;
            $data['data']="删除工位成功！";
            echo json_encode($data);
        }
    }
      
}
/**
 * [修改权限]
 * @return [json] [description]
 */
public function modify(){
    $trem=M('trem');
     $id=$_POST['id'];
        
       if($_POST['id']){

        $map['id'] = $id;

        $trem -> create(); //收集post表单数据
           $result = $trem->where($map)->save();
           if(false !== $result)

           echo '修改工位成功！';
           else
            echo '修改工位失败！';       
           }       
       else
       {
            echo '没有选择工位！'; 
       }
     
}

}